package com.yuda.afterexamscore.controller;

import com.yuda.afterexamscore.domain.SpartaSubject;
import com.yuda.afterexamscore.service.ISpartaSubjectService;
import com.yuda.common.core.utils.poi.ExcelUtil;
import com.yuda.common.core.web.controller.BaseController;
import com.yuda.common.core.web.domain.AjaxResult;
import com.yuda.common.core.web.page.TableDataInfo;
import com.yuda.common.log.annotation.Log;
import com.yuda.common.log.enums.BusinessType;
import com.yuda.common.security.annotation.PreAuthorize;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 公共学科Controller
 *
 * @author wyn
 * @date 2021-05-18
 */
@RestController
@RequestMapping("/spartaSubject")
@RefreshScope
public class SpartaSubjectController extends BaseController
{
    @Autowired
    private ISpartaSubjectService spartaSubjectService;

    /**
     * 查询公共学科列表
     */
//    @PreAuthorize(hasPermi = "common:subject:list")
    @GetMapping("/list")
    @ApiOperation("分页获取科目信息")
    public TableDataInfo list(SpartaSubject spartaSubject)
    {
        startPage();
        List<SpartaSubject> list = spartaSubjectService.selectSpartaSubjectList(spartaSubject);
        return getDataTable(list);
    }

    /**
     * 导出公共学科列表
     */
    @PreAuthorize(hasPermi = "common:subject:export")
    @Log(title = "公共学科", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SpartaSubject spartaSubject) throws IOException
    {
        List<SpartaSubject> list = spartaSubjectService.selectSpartaSubjectList(spartaSubject);
        ExcelUtil<SpartaSubject> util = new ExcelUtil<SpartaSubject>(SpartaSubject.class);
        util.exportExcel(response, list, "subject");
    }

    /**
     * 获取公共学科详细信息
     */
    @PreAuthorize(hasPermi = "common:subject:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(spartaSubjectService.selectSpartaSubjectById(id));
    }

    /**
     * 新增公共学科
     */
    @PreAuthorize(hasPermi = "common:subject:add")
    @Log(title = "公共学科", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SpartaSubject spartaSubject)
    {
        return toAjax(spartaSubjectService.insertSpartaSubject(spartaSubject));
    }

    /**
     * 修改公共学科
     */
    @PreAuthorize(hasPermi = "common:subject:edit")
    @Log(title = "公共学科", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SpartaSubject spartaSubject)
    {
        return toAjax(spartaSubjectService.updateSpartaSubject(spartaSubject));
    }

    /**
     * 删除公共学科
     */
    @PreAuthorize(hasPermi = "common:subject:remove")
    @Log(title = "公共学科", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(spartaSubjectService.deleteSpartaSubjectByIds(ids));
    }
}
